//
//  XMAvatorController.m
//  XiaoMiao
//
//  Created by 23 on 2018/1/28.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMAvatorController.h"
#import "XMPersonInfoController.h"
#import "XMWebViewController.h"
#import "FeedbackController.h"
#import "AlertPwdController.h"

typedef NS_ENUM(NSInteger, XMAvatorOperatorType) {
    XMAvatorOperatorTypeAvator                      = 1000, //个人信息
    XMAvatorOperatorTypePass                        = 1001, //修改密码
    XMAvatorOperatorTypeHelp                        = 1002, //帮助中心
    XMAvatorOperatorTypeAbout                       = 1003, //关于我们
    XMAvatorOperatorTypeContact                     = 1004, //联系客服
    XMAvatorOperatorTypeLogout                      = 1005, //退出登录
};

@interface XMAvatorController ()

@property (weak, nonatomic) IBOutlet UIView *avatorView;
@property (weak, nonatomic) IBOutlet UIView *alterPwdView;
@property (weak, nonatomic) IBOutlet UIView *helpView;
@property (weak, nonatomic) IBOutlet UIView *aboutView;
@property (weak, nonatomic) IBOutlet UIView *contactView;
@property (weak, nonatomic) IBOutlet UIView *logoutView;

@property (weak, nonatomic) IBOutlet UIImageView *avatorImageView;
@property (weak, nonatomic) IBOutlet UILabel *accountLabel;

@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;

@end

@implementation XMAvatorController

- (void)dealloc {
    [AppNotificationHelpper removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
    
    [self request_phoneDataIsCall:NO];
    
    [AppNotificationHelpper addObserver:self selector:@selector(infoChangeNoti) name:@"infoChangeNoti" object:nil];
}


#pragma mark - UI
- (void)setupUI
{
    self.title = @"个人中心";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"意见反馈" style:UIBarButtonItemStyleDone target:self action:@selector(actionFeedback)];
//    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} forState:UIControlStateNormal];
    
    //
    LoginInfoModel *infoModel = [LoginInfoModel getLoginInfo];
    
    self.avatorImageView.layer.cornerRadius = 4.0f;
    self.avatorImageView.layer.masksToBounds = YES;
    [self.avatorImageView sd_setImageWithURL:[NSURL URLWithString:infoModel.profile] placeholderImage:kDefaultPlaceholdeImage];
    self.accountLabel.text = infoModel.realName;
    
    //
    self.avatorView.tag = XMAvatorOperatorTypeAvator;
    [self.avatorView addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMenu:)] ];
    
    self.alterPwdView.tag = XMAvatorOperatorTypePass;
    [self.alterPwdView addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMenu:)] ];
    
    self.helpView.tag = XMAvatorOperatorTypeHelp;
    [self.helpView addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMenu:)] ];
    
    self.aboutView.tag = XMAvatorOperatorTypeAbout;
    [self.aboutView addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMenu:)] ];
    
    self.contactView.tag = XMAvatorOperatorTypeContact;
    [self.contactView addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMenu:)] ];
    
    self.logoutView.tag = XMAvatorOperatorTypeLogout;
    [self.logoutView addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMenu:)] ];
    
}

#pragma mark - Noti
- (void)infoChangeNoti {
    [self setupUI];
}

#pragma mark - action
- (void)actionFeedback
{
    FeedbackController *feedback = [[FeedbackController alloc] init];
    [self.navigationController pushViewController:feedback animated:YES];
}

- (void)tapMenu:(UITapGestureRecognizer *)tap
{
    switch (tap.view.tag) {
        case XMAvatorOperatorTypeAvator:
        {
            XMPersonInfoController *vc = [[XMPersonInfoController alloc] initWithMine];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case XMAvatorOperatorTypePass:
        {
            AlertPwdController *pwd = [[AlertPwdController alloc] init];
            [self.navigationController pushViewController:pwd animated:YES];
        }
            break;
        case XMAvatorOperatorTypeHelp:
        {
            XMWebViewController *vc = [[XMWebViewController alloc] init];
            vc.type = XMWebViewTypeHelp;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case XMAvatorOperatorTypeAbout:
        {
            XMWebViewController *vc = [[XMWebViewController alloc] init];
            vc.type = XMWebViewTypeAbout;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case XMAvatorOperatorTypeContact:
        {
//            XMWebViewController *vc = [[XMWebViewController alloc] init];
//            vc.type = XMWebViewTypeContact;
//            [self.navigationController pushViewController:vc animated:YES];
            if ([NSString app_isBlankString:self.phoneLabel.text]) {
                [self request_phoneDataIsCall:YES];
            }else {
                [self callPhone:self.phoneLabel.text];
            }
        }
            break;
            
        case XMAvatorOperatorTypeLogout:
        {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"退出" message:@"确定要退出吗？" preferredStyle:UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self request_logout];
            }]];
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
            
            [self presentViewController:alert animated:YES completion:nil];
        }
            break;
        default:
            break;
    }
}

#pragma mark - Private
- (void)callPhone:(NSString *)phone {
//    NSURL *url = [NSURL URLWithString:@"tel://0951-7690553"];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", phone]];
    if([[UIApplication sharedApplication] canOpenURL:url]){
        [[UIApplication sharedApplication] openURL:url];
    }else{
        [KLProgressHUD showErrorHUDWithMessage:@"当前设备不支持通话"];
    }
}

#pragma mark - Request
- (void)request_logout {
    KLProgressHUD *progressHUD = [KLProgressHUD showWaitingHUDWithSuperView:self.view message:@"退出中.."];
    
    [AppNetwork getWithUrl:kLogoutUrl refreshCache:NO params:nil success:^(id response) {
        progressHUD.hidden = YES;
        [AppNotificationHelpper postNotificationName:kLogoutNotification object:nil];
        
    } fail:^(AppError *error) {
        progressHUD.hidden = YES;
        [KLProgressHUD showErrorHUDWithMessage:[error errorMessage]];
    }];
}

- (void)request_phoneDataIsCall:(BOOL)isCall {
    NSString *url = [NSString stringWithFormat:@"%@/api/biz/gl/v1/glConfigs/get.htm",kApiServer];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"key"] = @"contact";
    
    WEAKSELF
    if (isCall) {
        [KLProgressHUD showWaitingHUDWithSuperView:self.view];
    }
    
    [AppNetwork getWithUrl:url refreshCache:NO params:parameters success:^(id response) {
        [KLProgressHUD hideHUDForView:self.view animated:YES];
        NSDictionary *glConfigViews = response[@"glConfigViews"];
        NSString *content = glConfigViews[@"content"];
//        NSString *name = glConfigViews[@"name"];
        
        weakSelf.phoneLabel.text = content;
        
        if (isCall) {
            [weakSelf callPhone:content];
        }
        
    } fail:^(AppError *error) {
        [KLProgressHUD hideHUDForView:self.view animated:YES];
        [KLProgressHUD showErrorHUDWithError:error];
    }];
}

@end
